package inits

import (
	"fmt"
	"goods_srv/basic/config"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
)

func InitMysql() {
	var err error
	cfg := config.AppCong.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})

	if err != nil {
		panic(err)
	}
	log.Println("mysql inits success")

	//// 自动迁移兴趣相关表
	//if err := models.AutoMigrateInterest(); err != nil {
	//	log.Println("auto migrate interest error:", err)
	//}
	//
	//// 初始化十个兴趣种子数据（如果不存在）
	//seedInterests := []string{"音乐", "电影", "旅行", "美食", "运动", "阅读", "摄影", "游戏", "科技", "宠物"}
	//for i, name := range seedInterests {
	//	var count int64
	//	if err := config.DB.Model(&models.Interest{}).Where("name = ?", name).Count(&count).Error; err == nil && count == 0 {
	//		_ = config.DB.Create(&models.Interest{Name: name, Sort: int32(i)}).Error
	//	}
	//}
}
